var ACTION_ADD = 0;
var ACTION_REMOVE = 1;

function ModifyItems(oInputGrid, oRefreshGrid, aTargetIds, action)
{
if (oInputGrid)
{

var aTargetIds = getSelected(oInputGrid);
if (!aTargetIds)
{
alert(LOCID_ACTION_NOITEMSELECTED);
return false;
}
}

var targetids = "";
var memberXml = "";
var oTypeToAdd = "4000";
var lookupType = "ResourceGroupItems";
var lookupSelectionStatus = "multi";
var oTypesToAdd = formatString("{0},{1},{2},{3}", SystemUser, Team, Equipment, ResourceGroup);

for (i=0; i<aTargetIds.length; i++)
{
targetids += aTargetIds[i] + ";";
}

var retval = LookupObjects(null, lookupSelectionStatus, lookupType, oTypesToAdd, 0);
var retvals = "";

if (!retval || retval.items.length == 0)
{

return false;
}
else
{


var aResourceGroups = new Array();
for (var i=0; i<retval.items.length; i++)
{
if (retval.items[i].type == ResourceGroup)
{
aResourceGroups.push(retval.items[i]);
}
else
{
retvals += retval.items[i].id + ";";
}
}

if (aResourceGroups.length > 0)
{
var aCircularFlags = HandleCircularReferences(aTargetIds, aResourceGroups);
var aCircularKidsNames = new Array();

for (i=0; i < aResourceGroups.length; i++)
{




if (aCircularFlags[i] == true)
{

aCircularKidsNames.push(aResourceGroups[i].name);
}
else
{

retvals += aResourceGroups[i].id + ";";
}
}

if (aCircularKidsNames.length > 0)
{

ShowCycleError(aCircularKidsNames);
}
}


if (retvals.length > 0)
{



targetids = targetids.substr(0, targetids.length-1);
retvals = retvals.substr(0, retvals.length-1);

memberXml = "<membership>";

memberXml += "<resources>" + retvals + "</resources>";

memberXml += "<resourcegroups>" + targetids + "</resourcegroups></membership>";

var oXml = CreateXmlDocument(false);
oXml.loadXML(memberXml);

var submitUrl = "";

if (action == ACTION_ADD)
{
submitUrl = prependOrgName("/_grid/cmds/cmd_additems.aspx");
}
else if (action == ACTION_REMOVE)
{
submitUrl = prependOrgName("/_grid/cmds/dlg_removeitems.aspx");
}

var xmlhttp = CreateXmlHttp();
xmlhttp.open("POST", submitUrl + "?iObjType=" + oTypeToAdd, false);
SetTokenInHeader(xmlhttp, submitUrl);
xmlhttp.send(oXml);





var oXml = CreateXmlDocument(false);
oXml.loadXML(xmlhttp.responseXML.xml);

if (oRefreshGrid && handleXMLErr(oXml))
{
document.all[oRefreshGrid].Refresh();
}
}
}
}

function HandleCircularReferences(aParentGuids, aChildren)
{

aParentGuids.type = "guid";


var aChildGuids = new Array();
for (var i=0; i < aChildren.length; i++)
{
aChildGuids.push( aChildren[i].id);
}
aChildGuids.type = "guid";


var aReturnItems = new Array();

var oCommand = new RemoteCommand("ResourceGroupUI", "IsResourceGroupCycle");
if (oCommand != null)
{
oCommand.SetParameter("parentIds", aParentGuids);
oCommand.SetParameter("childCandidates", aChildGuids);

var oResult = oCommand.Execute();
if (oResult.Success)
{


if (isArray(oResult.ReturnValue.boolean))
{
aReturnItems = oResult.ReturnValue.boolean;
}
else
{
aReturnItems.push(oResult.ReturnValue.boolean);
}
}

}
return aReturnItems;
}

function ShowCycleError(aCircularKidsNames)
{


var sUrl = prependOrgName("/SM/ResourceGroups/cycledialog.aspx?cyclecount=" + aCircularKidsNames.length);


var oResult = openStdDlg(sUrl, aCircularKidsNames, 460, 270);

return oResult;
}

function RemoveItems(sGrid, sGroupId)
{
var aTargetIds = getSelected(sGrid);
if (aTargetIds.length > 0)
{
if (openStdDlg(prependOrgName("/_grid/cmds/dlg_removeitems.aspx?iTotal=") + aTargetIds.length + "&groupid=" + sGroupId, aTargetIds, 400, 200))
{
document.all[sGrid].Refresh();
}
}
else
{
alert( LOCID_ACTION_NOITEMSELECTED );
}
}